var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    input_list:[
      {
        "type": "text",
        "title": "菜名",
        "name": "name"
      },
      {
        "type": "picker1",
        "title": "星期",
        "name": "week",
        "range": ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
        "value": 0
      },
      {
        "type": "picker1",
        "title": "类型",
        "name": "type",
        "range": ["早餐", "午餐", "晚餐"],
        "value": 0
      },
      {
        "type": "top"
      },
      {
        "type": "pic",
        "title": "图片",
        "name": "img",
        "num": 1,
        "pics": []
      }
    ]
  },
  bindUploadImg: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    var that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    wx.chooseImage({
      count: data.num > 1 ? 9 : 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中……',
        })
        tempFilePaths.forEach(function (item, idx) {
          wx.uploadFile({
            url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + token,
            filePath: item,
            name: 'file',
            success: function (res) {
              var out = JSON.parse(res.data);
              console.log(out);
              if (out.err_code > 0) {
                wx.showToast({
                  title: '上传失败！',
                })
              } else {
                list.forEach(function (value, index) {
                  console.log(value);
                  console.log(data);
                  if (value.title == data.title) {
                    var pics = list[index].pics;
                    var file_name = out.data.domain;
                    if (data.num > 1) {
                      pics.push(file_name);
                    } else {
                      pics = [file_name];
                    }
                    list[index].pics = pics;
                  }
                })
                that.setData({
                  input_list: list
                })
              }

              if (idx == tempFilePaths.length - 1) {
                wx.hideLoading();
              }
              //do something
            }
          })
        })
      }
    })
  },
  bindChange: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].value = e.detail.value;
      }
    })
    this.setData({
      input_list: list
    })
  },
  formSubmit: function(e){
    app.addFormId(e.detail.formId);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    data.input_list.forEach(function (item, idx) {
      if (item.type == 'pic') {
        post_data[item.name] = item.pics;
        post_data.img_url = item.pics[0];
      }
    })
    post_data.openid = app.globalData.openid;
    // post_data.type = type;
    app.ihttpPost("https://www.58zltc.com/api.php/Jiaoyu/foodAdd", post_data, res=>{
      if (res.data.err_code > 0) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        wx.showToast({
          title: res.data.err_msg,
          icon: 'success'
        })
        setTimeout(function () {
          wx.navigateBack({})
        }, 500)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //type = options.type;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  backto: function () {
    wx.navigateBack({
      
    })
  }
})